package org.hospital.service.impl;


import java.text.SimpleDateFormat;
import java.util.List;

import org.hospital.entity.Clinic;
import org.hospital.entity.ClinicDAO;
import org.hospital.entity.Department;
import org.hospital.entity.DepartmentDAO;
import org.hospital.entity.Employee;
import org.hospital.entity.EmployeeDAO;
import org.hospital.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

@Service
public class DepartmentServiceImpl implements DepartmentService {

	@Autowired
	private DepartmentDAO dpmDao;
	@Autowired
	private EmployeeDAO eDao;
	@Autowired
	private ClinicDAO cDao;
	
	
	@Override
	public Department getByid(int departmentId) {
		return dpmDao.findById(departmentId);
	}

	@Override
	public Department addEntity(Department entity) {
		return dpmDao.merge(entity);
	}

	@Override
	public Department saveEntity(Department entity) {
		return dpmDao.merge(entity);
	}

	@Override
	public JSONArray getClinicAllDepartment(int clinicId) {
		JSONArray resultJA = new JSONArray();
		List<Department> dpmList = dpmDao.findByClinicId(clinicId);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for (int i = 0; i < dpmList.size(); i++) {
			Department dpm = dpmList.get(i);
			Clinic clinic = cDao.findById(dpm.getClinicId());
			Employee creator = eDao.findById(dpm.getCreatorId());
			Employee principal =  eDao.findById(dpm.getEmployeeId());
			
			JSONObject resultJO = new JSONObject();
			resultJO.put("clinicId", clinic.getClinicId());
			resultJO.put("clinicName", clinic.getName());
			resultJO.put("departmentId", dpm.getDepartmentId());
			resultJO.put("departmentName", dpm.getName());
			resultJO.put("creatorId", creator.getEmployeeId());
			resultJO.put("creatorName", creator.getName());
			resultJO.put("clinicprincipalId", clinic.getEmployeeId());
			resultJO.put("principalId", principal.getEmployeeId());
			resultJO.put("principalCode", principal.getEmployeeCode());
			resultJO.put("principalName", principal.getName());
			resultJO.put("phone", dpm.getPhone());
			resultJO.put("createTime", sdf.format(dpm.getCreateTime()));
			resultJA.add(resultJO);
		}
		return resultJA;
	}

	@Override
	public JSONArray getAllDepartment() {
		JSONArray resultJA = new JSONArray();
		List<Department> dpmList = dpmDao.findAll();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for (int i = 0; i < dpmList.size(); i++) {
			Department dpm = dpmList.get(i);
			Clinic clinic = cDao.findById(dpm.getClinicId());
			Employee creator = eDao.findById(dpm.getCreatorId());
			Employee principal =  eDao.findById(dpm.getEmployeeId());
			
			JSONObject resultJO = new JSONObject();
			resultJO.put("clinicId", clinic.getClinicId());
			resultJO.put("clinicName", clinic.getName());
			resultJO.put("departmentId", dpm.getDepartmentId());
			resultJO.put("departmentName", dpm.getName());
			resultJO.put("creatorId", creator.getEmployeeId());
			resultJO.put("creatorName", creator.getName());
			resultJO.put("clinicprincipalId", clinic.getEmployeeId());
			resultJO.put("principalId", principal.getEmployeeId());
			resultJO.put("principalCode", principal.getEmployeeCode());
			resultJO.put("principalName", principal.getName());
			resultJO.put("phone", dpm.getPhone());
			resultJO.put("createTime", sdf.format(dpm.getCreateTime()));
			resultJA.add(resultJO);
		}
		return resultJA;
	}


}
